package com.ihd.ihardware.view.enter.view;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.ihd.ihardware.R;
import com.ihd.ihardware.app.MyApplication;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.databinding.ActivityPerBinding;
import com.ihd.ihardware.pojo.MembersRes;
import com.ihd.ihardware.pojo.UserInfoRes;
import com.ihd.ihardware.view.enter.viewModel.SplashViewModel;
import com.ihd.ihardware.view.tzc.main.view.BalanceActivity;
import com.ihd.ihardware.view.tzc.me.view.PerInfoActivity;

import java.io.File;

import cn.wowjoy.commonlibrary.app.Constans;
import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.utils.BLEUtils;
import cn.wowjoy.commonlibrary.utils.HttpUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.utils.ToastUtils;

public class SplashActivity extends BaseActivity<ActivityPerBinding, SplashViewModel> {

    private int REQUEST_CODE_BLUETOOTH_ON = 100;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(false).init();

        if (!isTaskRoot()) {
            Intent intent = getIntent();
            if (intent != null) {
                String action = intent.getAction();
                if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(action)) {
                    finish();
                    return;
                }
            }
        }

        File f = new File(AppConstans.BASE_PATH);
        if (!f.exists())
            f.mkdirs();

        if (!BLEUtils.getInstance().isBluetoothOpened()) {
            Intent requestBluetoothOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(requestBluetoothOn, REQUEST_CODE_BLUETOOTH_ON);
        } else {
            connectBluetooth();
        }

        setRx(AppConstans.INFO, new BaseConsumer<UserInfoRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(UserInfoRes mLiveData) {
                MyApplication.memberId = new MembersRes.DataBean();
                MyApplication.memberId.setMemberId("");

                if (TextUtils.isEmpty(mLiveData.getData().getWeight())) {
                    Intent mIntent = new Intent(SplashActivity.this, PerInfoActivity.class);
                    mIntent.putExtra("userinfo", mLiveData.getData());
                    startActivity(mIntent);
                } else {
                    SPUtils.putString(AppConstans.USER_INFO, new Gson().toJson(mLiveData.getData(), UserInfoRes.DataBean.class));
                    Intent mIntent = new Intent(SplashActivity.this, BalanceActivity.class);
                    startActivity(mIntent);
                }
                finish();
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(SplashActivity.this, error.getMessage());
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_BLUETOOTH_ON) {
            switch (resultCode) {
                case Activity.RESULT_OK:
                    connectBluetooth();
                    break;
                case Activity.RESULT_CANCELED:
                    ToastUtils.showLong(this, "请手动打开蓝牙进入app，才能获取数据哦~");
                    connectBluetooth();
                    break;
                default:
                    break;
            }
        }
    }

    private void connectBluetooth() {
        if (TextUtils.isEmpty(SPUtils.getString(Constans.GATEWAY_TOKEN, ""))) {
            Intent i = new Intent(SplashActivity.this, PerActivity.class);
            startActivity(i);
            finish();
        } else {
            if (HttpUtils.getInstance().isNetworkConnected())
                viewModel.getUserInfo();
            else {
                String info = SPUtils.getString(AppConstans.USER_INFO, "");
                if (!TextUtils.isEmpty(info)) {
                    Intent mIntent = new Intent(SplashActivity.this, BalanceActivity.class);
                    startActivity(mIntent);
                } else {
                    Intent i = new Intent(SplashActivity.this, PerActivity.class);
                    startActivity(i);
                }
                finish();
            }
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected Class getViewModelClass() {
        return SplashViewModel.class;
    }

}
